package com.dognessnetwork.customer.service.impl;

import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

@Service
public class RestService {
    private   final   Logger   log =   LoggerFactory.getLogger(this.getClass());

	@Autowired
    private RestTemplate restTemplate;
	
	/**
	 * 获取用户登录时的x_auth_token
	 * @param token
	 * @return
	 */
	public	String	get_login_x_auth_token(String	token,String	username,String	p){
		HttpHeaders httpHeaders = new HttpHeaders();
		httpHeaders.add("x-auth-token", token);
        httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
		//用户登录
        HttpEntity<String> entity = new HttpEntity<String>(httpHeaders);
        String url="http://119.29.172.17:8080/authentication/login?username="+username+"&password="+p;
        HttpHeaders	HttpHeaders	=	restTemplate.exchange(url, HttpMethod.POST, entity, String.class).getHeaders();
        String	x_auth_token	= 	HttpHeaders.getFirst("x-auth-token");
        return	x_auth_token;
	}
	
	/**
	 * 获取用户登录信息
	 * @param x_auth_token
	 * @return
	 */
	public	JSONObject	getLoginUser(String	x_auth_token){
		HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("x-auth-token", x_auth_token);
        String result = HttpUtil.post("http://119.29.172.17:8080/getLoginUser", paramMap);
        log.info("\n用户登录信息{}",result);
        JSONObject	userInfo	=	JSONUtil.parseObj(result);
		return	userInfo;
	}
}
